package cn.lg.soar.boot.nc.service.impl;

import cn.lg.soar.boot.nc.entity.vo.PrecautionsVo;
import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.model.IPageQuery;
import cn.lg.soar.common.model.PageQuery;
import cn.lg.soar.common.model.PageResult;
import cn.lg.soar.common.util.ParameterUtil;
import cn.lg.soar.database.query.QueryParams;
import cn.lg.soar.database.query.SqueryWapper;
import cn.lg.soar.database.util.PageUtils;
import cn.lg.soar.system.api.service.IUserApi;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.lg.soar.database.base.service.impl.BaseServiceImpl;
import cn.lg.soar.boot.nc.mapper.PrecautionsMapper;
import cn.lg.soar.boot.nc.entity.Precautions;
import cn.lg.soar.boot.nc.service.PrecautionsService;

import java.io.Serializable;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 注意事项表
 * @author BoBo
 * @since 2025-02-17
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Service
public class PrecautionsServiceImpl extends BaseServiceImpl<PrecautionsMapper, Precautions> implements PrecautionsService {

    //获取用户名的方法
    @Autowired
    IUserApi iUserApi;

    private SqueryWapper<Precautions> precautionsSqueryWapper;

    @Override
    public HttpResult<Serializable> updateSort(List<Map<String, BigInteger>> sortedData) {
        if (null == sortedData || 0 >= sortedData.size()) {
            return HttpResult.fail("4000", "排序数据不可为空", "error");
        }
        sortedData.forEach(item-> {
            if (!item.containsKey("id") || !item.containsKey("orders")) {
                return;
            }
            Long id = item.get("id").longValue(); //类型转换
            int orders = item.get("orders").intValue(); //类型转换
            Precautions precautions = new Precautions();
            precautions.setId(id);
            precautions.setOrders(orders);
            //执行修改
            super.updateById(precautions);
        });
        return HttpResult.success("排序成功");
    }

    @Override
    public HttpResult<PageResult<PrecautionsVo>> getPrecautionsVo(PageQuery pageQuery, QueryParams params) {
        params.setFields(new ArrayList<>());
        PageResult<PrecautionsVo> resultVo = new PageResult<>();

        if (precautionsSqueryWapper == null){
            precautionsSqueryWapper = new SqueryWapper<>(Precautions.class);
        }
        QueryWrapper<Precautions> precautionsQueryWrapper = Wrappers.query();
        if (pageQuery==null || pageQuery.getPageSize()<=0) {
            precautionsSqueryWapper.setWhere(params.getF(),precautionsQueryWrapper);
            precautionsSqueryWapper.setSelect(params.getFields(),precautionsQueryWrapper);
            precautionsSqueryWapper.setOrderBy(params.getOrderBy(), precautionsQueryWrapper);
            //所有数据
            List<Precautions> list = super.list(precautionsQueryWrapper);
            //vo数据
            List<PrecautionsVo> voList = new ArrayList<>();
            for (Precautions precautions : list) {
                PrecautionsVo vo = new PrecautionsVo();
                BeanUtils.copyProperties(precautions, vo);
                //获取用户名，并存入vo
                vo.setUpdateBy(iUserApi.getById(precautions.getUpdateBy()).getNickname());
                voList.add(vo);
            }
            resultVo.setList(voList);
            return HttpResult.data(resultVo);
        }
        resultVo.setPageNo(pageQuery.getPageNo());
        resultVo.setPageSize(pageQuery.getPageSize());
        resultVo.setQueryType(pageQuery.getQueryType());


        if (IPageQuery.QueryType.total.equals(pageQuery.getQueryType())) {
            precautionsSqueryWapper.setWhere(params.getF(),precautionsQueryWrapper);
            resultVo.setTotal(super.count(precautionsQueryWrapper));
        }
        IPage<Precautions> page = PageUtils.toPage(pageQuery);
        precautionsSqueryWapper.setWhere(params.getF(),precautionsQueryWrapper);
        precautionsSqueryWapper.setSelect(params.getFields(),precautionsQueryWrapper);
        precautionsSqueryWapper.setOrderBy(params.getOrderBy(), precautionsQueryWrapper);

        page = super.page(page, precautionsQueryWrapper);
        resultVo.setTotal(page.getTotal());

        //vo数据
        List<PrecautionsVo> voList = new ArrayList<>();
        //所有数据
        List<Precautions> records = page.getRecords();
        for (Precautions record : records) {
            PrecautionsVo vo = new PrecautionsVo();
            BeanUtils.copyProperties(record, vo);
            vo.setUpdateBy(iUserApi.getById(record.getUpdateBy()).getNickname());
            voList.add(vo);
        }
        //塞入模型数据
        resultVo.setList(voList);

        return HttpResult.data(resultVo);
    }

    @Override
    public HttpResult<PrecautionsVo> getById(Long id) {
        ParameterUtil.notNull(id, "缺少数据id");
        //返回数据
        PrecautionsVo vo = new PrecautionsVo();
        //所有数据
        Precautions precautions = super.getById(id);
        //复制数据
        BeanUtils.copyProperties(precautions, vo);
        //获取用户并存入vo;
        vo.setUpdateBy(iUserApi.getById(precautions.getUpdateBy()).getNickname());

        return HttpResult.data(vo);
    }
}
